<?php


namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class UsersRating extends Model
{

    protected $connection = 'mysql';
    public $table = 'users_rating';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'new_weight', 'ole_height','score','grade','ole_grade','sys_user','sys_user_dt','payment_rate_weight',
        'payment_coin_weight','response_rate_weight','gift_weight','consumption_amount','cumulative_average','video_duration','created_at',
        'updated_at'
    ];


    const HOST_CATE_A = 'T0';
    const HOST_CATE_B = 'T1';
    const HOST_CATE_C = 'T2';
    const HOST_CATE_D = 'T3';

    const HOST_CATE = [
        self::HOST_CATE_A => 'T0',
        self::HOST_CATE_B => 'T1',
        self::HOST_CATE_C => 'T2',
        self::HOST_CATE_D => 'T3',
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    public static function updateGrade($userId =0 ,$value = 'T1')
    {

        $info = self::where(['user_id' => $userId])->first();
        if($info){
            $info->grade = $value;
            $info->save();
            return true;
        }
        self::create([
            'user_id' => $userId,
            'new_weight' => 0,
            'ole_height' => 0,
            'score' => 0,
            'grade' => $value,
            'ole_grade' => 0,
            'sys_user' => 0,
            'sys_user_dt' => 0,
            'payment_rate_weight' => 0,
            'payment_coin_weight' => 0,
            'response_rate_weight' => 0,
            'gift_weight' => 0,
            'consumption_amount' => 0,
            'cumulative_average' => 0,
            'video_duration' => 0,
        ]);
    }
}
